﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAO;
using DTO;
using PTExpress.Models;
using System.Web.Routing;

namespace PTExpress.Controllers
{
    public abstract class AbstractController : Controller
    {
        protected NhanVienDTO nhanVienDTO;

        public IFormsAuthenticationService FormsService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            if (FormsService == null)
            {
                FormsService = new FormsAuthenticationService();
            }

            if (this.HttpContext.Request.IsAuthenticated)
            {
                string userName = this.User.Identity.Name;
                NhanVienDAO nvDAO = new NhanVienDAO();
                nhanVienDTO = nvDAO.getByTenDangNhap(userName);
            }
        }


        protected AbstractController()
            : base()
        {
            XeDAO xeDao = new XeDAO();
            List<XeDTO> lsXe = xeDao.GetAllWhere("daxoa = 0");
            List<SelectListItem> lsXeItem = new List<SelectListItem>();
            lsXeItem.Add(new SelectListItem());
            foreach (XeDTO xe in lsXe)
            {
                SelectListItem item = new SelectListItem();
                item.Text = xe.TenXe;
                item.Value = xe.Id.ToString();
                lsXeItem.Add(item);
            }
            ViewData["lsEXe"] = lsXeItem;

            TuyenDuongDAO tuyenDuongDAO = new TuyenDuongDAO();
            List<TuyenDuongDTO> lsTuyenDuong = tuyenDuongDAO.GetAll();
            List<SelectListItem> lsTuyenDuongItem = new List<SelectListItem>();
            lsTuyenDuongItem.Add(new SelectListItem());
            foreach (TuyenDuongDTO xe in lsTuyenDuong)
            {
                SelectListItem item = new SelectListItem();
                item.Text = string.Format("{0} đi {1}", xe.NoiDi, xe.NoiDen);
                item.Value = xe.Id.ToString();
                lsTuyenDuongItem.Add(item);
            }
            ViewData["lsETuyenDuong"] = lsTuyenDuongItem;

            KhachHangDAO khachHangDAO = new KhachHangDAO();
            List<KhachHangDTO> lsKhachHang = khachHangDAO.GetAllUnCommited();
            List<SelectListItem> lsKhachHangItem = new List<SelectListItem>();
            lsKhachHangItem.Add(new SelectListItem());
            foreach (KhachHangDTO kh in lsKhachHang)
            {
                SelectListItem item = new SelectListItem();
                item.Text = kh.Hoten;
                item.Value = kh.Id.ToString();
                lsKhachHangItem.Add(item);
            }
            ViewData["lsEKhachHang"] = lsKhachHangItem;

            ChuyenXeDAO chuyenXeDAO = new ChuyenXeDAO();
            List<ChuyenXeDTO> lsChuyenXe = chuyenXeDAO.GetAll();
            List<SelectListItem> lsChuyenXeItem = new List<SelectListItem>();
            lsChuyenXeItem.Add(new SelectListItem());
            foreach (ChuyenXeDTO cx in lsChuyenXe)
            {
                SelectListItem item = new SelectListItem();
                item.Text = string.Format("{0} ngày {1}", cx.TenTuyenDuong, cx.XuatPhat.Date);
                item.Value = cx.Id.ToString();
                lsChuyenXeItem.Add(item);
            }
            ViewData["lsEChuyenXe"] = lsChuyenXeItem;


        }
    }
}